<script setup>
    import adminApi from "@/api/admin/admin.js";

    import {ElMessage} from 'element-plus'
    import {useRouter} from 'vue-router'
    const router = useRouter();

    import {useTokenStore} from "@/store/token.js";
    const tokenStore = useTokenStore();
    import {useAdminInfoStore} from "@/store/adminInfo.js";
    import {ref} from "vue";

    const adminInfoStore = useAdminInfoStore();

    const titleName = "修改密码"

    const getAdminInfo = () => {
        adminApi.adminInfo().then(result => {
            if (result.code === 0) {
                adminInfoStore.setUserInfo(result.data)
            }
        })
    }
    getAdminInfo()

    const admin = ref({})

    const headers = ref({
        //添加token
        Authorization: tokenStore.token
    })

    //重置密码
    const adminPassword = ref({})
    const resetFormRef = ref()

    adminPassword.value = {}
    const rePasswordValid = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请重新输入密码'))
        } else if (value !== adminPassword.value.newPassword) {
            callback(new Error('两次输入新密码不一致'))
        } else {
            callback()
        }
    }

    //表单正则表达式
    const rules = ref({
        oldPassword: [
            {required: true, message:'请输入旧密码', trigger:"blur"},
            {min: 6, max: 16, message: '旧密码长度必须为6~16位', trigger:"blur"},
        ],
        newPassword: [
            {required: true, message: '请输入新密码', trigger:"blur"},
            {min: 6, max: 16, message: '密码长度必须为6~16位', trigger:"blur"},
        ],
        reNewPassword: [
            {required: true, message: '请再次输入新密码', trigger:"blur"},
            {validator: rePasswordValid, trigger:"blur"},
        ]
    })

    const resetPassword = (formEl) => {
        if (!formEl) return
        formEl.validate((valid) => {
            if (valid) {
                adminApi.resetPassword(adminPassword.value).then(result => {
                    if (result.code === 0) {
                        ElMessage({message: result.msg, type: 'success',})
                        //退出登录
                        tokenStore.removeToken()
                        adminInfoStore.removeAdminInfo()
                        //跳转到登录界面
                        router.push('/login')
                    } else {
                        ElMessage.error(result.msg)
                    }
                })
            } else {
                ElMessage.error('表单验证失败')
            }
        })
    }
</script>

<template>
    <el-card class="">
        <template #header>
            <div class="header">
                <strong>{{titleName}}</strong>
            </div>
        </template>

        <el-form ref="resetFormRef" :rules="rules" :model="adminPassword">
            <el-form-item prop="oldPassword" label="原密码" :label-width="100">
                <el-input v-model="adminPassword.oldPassword" autocomplete="off"/>
            </el-form-item>
            <el-form-item prop="newPassword" label="新密码" :label-width="100">
                <el-input v-model="adminPassword.newPassword" autocomplete="off"/>
            </el-form-item>
            <el-form-item prop="reNewPassword" label="重复新密码" :label-width="100">
                <el-input v-model="adminPassword.reNewPassword" autocomplete="off"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="resetPassword(resetFormRef)">
                    确认
                </el-button>
            </div>
        </template>
    </el-card>
</template>

<style scoped>

</style>